package com.example.shop_2302.xq

import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddCarEntity
import com.example.base.entity.PreOrderEntity
import com.example.base.entity.Res
import com.example.base.entity.XqEntity
import com.example.base.mvvm.BaseViewModel
import com.example.base.param.ParamPreOrder
import com.example.shop_2302.utils.SingleLiveData
import kotlinx.coroutines.launch

class XqViewModel:BaseViewModel() {
    //返回值--详情
    val xqSuccess = SingleLiveData<Res<XqEntity>>()
    val xqFail = SingleLiveData<String>()
    fun xq(id:Int,type:String){
        viewModelScope.launch {
            try {
                xqSuccess.postValue(apiServer.xq(id, type))
            }catch (e:Exception){
                xqFail.postValue(e.message.toString())
            }
        }
    }
    //返回值--新增购物车
    val addSuccess = SingleLiveData<Res<AddCarEntity>>()
    val addFail = SingleLiveData<String>()
    fun addCar(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addSuccess.postValue(apiServer.addCar(mapToBody(map)))
            }catch (e:Exception){
                addFail.postValue(e.message.toString())
            }
        }
    }
    //返回值--立即购买（预下单）
    val preSuccess = SingleLiveData<Res<PreOrderEntity>>()
    val preFail = SingleLiveData<String>()
    fun preOrder(paramPreOrder: ParamPreOrder){
        viewModelScope.launch {
            try {
                preSuccess.postValue(apiServer.preOrder(paramPreOrder))
            }catch (e:Exception){
                preFail.postValue(e.message.toString())
            }
        }
    }
}